package com.company.happy_number;

import java.util.HashSet;
import java.util.Set;

/**
 * link:https://leetcode-cn.com/problems/happy-number/
 */

public class Solution {

    private final Set<Integer> result = new HashSet<>();

    public boolean isHappy(int n) {
        int count = 0;
        while (n != 0) {
            int number = n % 10;
            count += Math.pow(number, 2);
            n = n / 10;
        }
        if (count == 1) return true;
        if (result.contains(count)) return false;
        result.add(count);
        return isHappy(count);
    }

    public static void main(String[] args) {
        // write your code here
        System.out.println(new Solution().isHappy(19));
    }
}
